package com.hckjsec.cloud.controller;

import java.util.ArrayList;
import java.util.List;

import com.hckjsec.cloud.service.WxSendTplMessageService;
import com.hckjsec.common.utils.DateUtils;
import com.hckjsec.common.utils.SecurityUtils;
import com.hckjsec.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hckjsec.common.annotation.Log;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.enums.BusinessType;
import com.hckjsec.cloud.domain.AppletsBusinessReserve;
import com.hckjsec.cloud.service.IAppletsBusinessReserveService;
import com.hckjsec.common.utils.poi.ExcelUtil;
import com.hckjsec.common.core.page.TableDataInfo;

/**
 * 申办信息预约Controller
 * 
 * @author cyt
 * @date 2021-09-03
 */
@RestController
@RequestMapping("/system/reserve")
public class AppletsBusinessReserveController extends BaseController
{
    @Autowired
    private IAppletsBusinessReserveService appletsBusinessReserveService;

    @Autowired
    private WxSendTplMessageService wxSendTplMessageService;
    /**
     * 查询申办信息预约列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppletsBusinessReserve appletsBusinessReserve)
    {
        startPage();
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsBusinessReserve.setDeptId(SecurityUtils.getDeptId());
        }
        List<AppletsBusinessReserve> list = appletsBusinessReserveService.selectAppletsBusinessReserveList(appletsBusinessReserve);
        return getDataTable(list);
    }

    /**
     * 导出申办信息预约列表
     */
    @Log(title = "申办信息预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppletsBusinessReserve appletsBusinessReserve)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            appletsBusinessReserve.setDeptId(SecurityUtils.getDeptId());
        }
        List<AppletsBusinessReserve> list = appletsBusinessReserveService.selectAppletsBusinessReserveList(appletsBusinessReserve);
        ExcelUtil<AppletsBusinessReserve> util = new ExcelUtil<AppletsBusinessReserve>(AppletsBusinessReserve.class);
        return util.exportExcel(list, "reserve");
    }

    /**
     * 获取申办信息预约详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(appletsBusinessReserveService.selectAppletsBusinessReserveById(id));
    }


    /**
     * 修改申办信息预约
     */
    @Log(title = "申办信息预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppletsBusinessReserve appletsBusinessReserve)
    {
        AppletsBusinessReserve newappletsBusinessReserve = appletsBusinessReserveService.selectAppletsBusinessReserveById(appletsBusinessReserve.getId());
        newappletsBusinessReserve.setReviewerresult(appletsBusinessReserve.getReviewerresult());
        newappletsBusinessReserve.setReviewer(SecurityUtils.getNickName());
        newappletsBusinessReserve.setReviewerdatetime(DateUtils.parseDate(DateUtils.getTime()));
        newappletsBusinessReserve.setReviewercontent(appletsBusinessReserve.getReviewercontent());
        String scheduletime=StringUtils.isHcNull(appletsBusinessReserve.getScheduletime(),"");
        if ("".equals(scheduletime) && "1".equals(appletsBusinessReserve.getReviewerresult())){
            scheduletime=newappletsBusinessReserve.getAppointmentdate()+" "+newappletsBusinessReserve.getAppointmenttime();
        }
        newappletsBusinessReserve.setScheduletime(scheduletime);
        int rows=appletsBusinessReserveService.updateAppletsBusinessReserve(newappletsBusinessReserve);
        if(rows > 0){//发送短信
            List<String> phonelist = new ArrayList<String>();
            phonelist.add("+86"+newappletsBusinessReserve.getPhone());
            if("1".equals(newappletsBusinessReserve.getReviewerresult())){
                wxSendTplMessageService.sendAddOrder(phonelist, newappletsBusinessReserve.getConfigId(),"6",DateUtils.getYearMonthDaysfmStr2(scheduletime),newappletsBusinessReserve.getMatter(),"");
            }else if("2".equals(newappletsBusinessReserve.getReviewerresult())){
                wxSendTplMessageService.sendAddOrder(phonelist, newappletsBusinessReserve.getConfigId(),"7",newappletsBusinessReserve.getMatter(),"","");
            }
        }
        return toAjax(rows);
    }


}
